<!-- 添加车辆服务entercar1.php -->
<!DOCTYPE html>
<html>

<head>
    <title>客户注册 | 租车服务</title>
</head>
<?php session_start(); ?>
<link rel="shortcut icon" type="image/png" href="assets/img/P.png.png">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/fonts/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/customerlogin.css">
<link rel="stylesheet" href="assets/w3css/w3.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>

<body>
    <!-- 导航栏 -->
    <?php
    include './nav.php';
    ?>

    <?php
    // 引入数据库连接文件
    require 'connection.php';
    // 连接数据库
    $conn = Connect();

    // 获取图片文件扩展名的函数
    function GetImageExtension($imagetype)
    {
        if (empty($imagetype)) return false;

        switch ($imagetype) {
            case 'assets/img/cars/bmp':
                return '.bmp';
            case 'assets/img/cars/gif':
                return '.gif';
            case 'assets/img/cars/jpeg':
                return '.jpg';
            case 'assets/img/cars/png':
                return '.png';
            default:
                return false;
        }
    }

    // 检查会话中的客户用户名
    $user_check = $_SESSION['login_client'];

    // 如果提交了表单
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // 从表单获取用户输入的车辆信息，并防止 SQL 注入攻击
        $car_name = $conn->real_escape_string($_POST['car_name']);
        $car_nameplate = $conn->real_escape_string($_POST['car_nameplate']);
        $ac_price = $conn->real_escape_string($_POST['ac_price']);
        $non_ac_price = $conn->real_escape_string($_POST['non_ac_price']);
        $ac_price_per_day = $conn->real_escape_string($_POST['ac_price_per_day']);
        $non_ac_price_per_day = $conn->real_escape_string($_POST['non_ac_price_per_day']);
        $car_availability = "yes";

        // 如果上传了图片文件
        if (!empty($_FILES["uploadedimage"]["name"])) {
            $file_name = $_FILES["uploadedimage"]["name"];
            $temp_name = $_FILES["uploadedimage"]["tmp_name"];
            $imgtype = $_FILES["uploadedimage"]["type"];
            // 获取图片文件扩展名
            $ext = GetImageExtension($imgtype);
            $imagename = $_FILES["uploadedimage"]["name"];
            $target_path = "assets/img/cars/" . $imagename;

            // 将上传的图片文件移动到指定目录
            if (move_uploaded_file($temp_name, $target_path)) {
                // 将车辆信息插入到数据库中
                $query = "INSERT into cars(car_name,car_nameplate,car_img,ac_price,non_ac_price,ac_price_per_day,non_ac_price_per_day,car_availability) 
                VALUES('" . $car_name . "','" . $car_nameplate . "','" . $target_path . "','" . $ac_price . "','" . $non_ac_price . "','" . $ac_price_per_day . "','" . $non_ac_price_per_day . "','" . $car_availability . "')";
                $success = $conn->query($query);
            }
        }

        // 查询刚插入的车辆信息的编号
        $query1 = "SELECT car_id from cars where car_nameplate = '$car_nameplate'";
        $result = mysqli_query($conn, $query1);
        $rs = mysqli_fetch_array($result, MYSQLI_BOTH);
        $car_id = $rs['car_id'];

        // 将车辆信息与客户关联起来并插入到数据库中
        $query2 = "INSERT into clientcars(car_id,client_username) values('" . $car_id . "','" . $user_check . "')";
        $success2 = $conn->query($query2);

        // 如果插入失败则返回错误信息，否则返回成功信息
        if (!$success) {
            echo '<div class="container"><div class="jumbotron" style="text-align: center;">车辆的车牌号已存在！' . $conn->error . '<br><br><a href="entercar.php" class="btn btn-default"> 返回 </a></div></div>';
        } else {
            echo '<div class="container"><div class="jumbotron" style="text-align: center;width: 550px;">车辆添加成功！<br><br><a href="entercar.php" class="btn btn-default"> 返回 </a></div></div>';
        }
    }
    ?>

</body>

<footer class="site-footer">
    <div class="container">
        <hr>
        <div class="row">
            <div class="col-sm-6">
                <h5>© <?php echo date("Y"); ?> 租车服务</h5>
            </div>
        </div>
    </div>
</footer>

</html>